package Test1;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class lian_xi_Demo2 extends Thread {
    /*
    痛死开启两个线程，共同获取1-100之间的数字，打印所有奇数
    */
    static Lock lock = new ReentrantLock();
    @Override
    public void run() {
        lock.lock();
        try {
            for (int i = 1; i <= 100; i++) {
                if (i % 2 != 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "打印了" + i);
                }
            }
        } finally {
            lock.unlock();
        }
    }
}
